package ro.pub.cs.stadium.platform;

import java.io.IOException;
import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Shell;

/**
 * Encapsulates the functionality of the About box
 * @author stefan
 *
 */
public class AboutWindow implements MouseListener, PaintListener{
	/**
	 * The location where to find the splash image
	 */
	private final String SPLASH_LOCATION = "artwork/splash.png";
	
	/**
	 * The window shell
	 */
	private Shell aboutWnd;
	
	/**
	 * The splash image
	 */
	private Image splashImage;
	
	public AboutWindow(Shell mainWnd) {
		aboutWnd = new Shell(mainWnd, SWT.APPLICATION_MODAL);
		aboutWnd.setText("About StaDiUM");
		
		// Loading the splash image
		try {
			InputStream splashStream = this.getClass().getResourceAsStream(SPLASH_LOCATION);
			splashImage = new Image(mainWnd.getDisplay(), splashStream);
			splashStream.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		
		Rectangle imgBounds = splashImage.getBounds();
		
		aboutWnd.setSize(imgBounds.width, imgBounds.height);
		
		//	 Centering the splash screen
		Rectangle splashRect = aboutWnd.getBounds();
		Rectangle displayRect = mainWnd.getDisplay().getBounds();
		
		int x = (displayRect.width - splashRect.width) / 2;
		int y = (displayRect.height - splashRect.height) / 2;
		
		aboutWnd.setLocation(x, y);
		
		// Adding the handlers
		
		aboutWnd.addMouseListener(this);
		aboutWnd.addPaintListener(this);
	}
	
	public int showModal() {
		aboutWnd.open();
		return 0;
	}
	
	// MouseListener interface

	public void mouseDoubleClick(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseDown(MouseEvent e) {
		aboutWnd.close();
		
	}

	public void mouseUp(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	// PaintListener interface

	public void paintControl(PaintEvent e) {
		GC gc = e.gc;
		
		if (splashImage != null)
			gc.drawImage(splashImage, 0, 0);
	}
}
